<?php

/*
=====================================================
 Generic Toolbox - by Carl Wells
-----------------------------------------------------
-----------------------------------------------------
 Copyright (c) 2009 Carl Wells
=====================================================
 THIS IS COPYRIGHTED SOFTWARE
 PLEASE READ THE LICENSE AGREEMENT
=====================================================
 File: system/display/navdisplay.class.php
-----------------------------------------------------
 Purpose: This file builds all visual aspects of the Admin Section.
 All browser output is managed by this file.
=====================================================
*/
class Navdisplay
{
     	// Display the main navigation for the Control Panel
  	// TO BE PULLED FROM A DB LATER A POSSIBILITY
	function display_main_admin_nav(){
	
		$nav = "";
		$nav .="<ul class=\"sexypanels\">\n";
		$nav .="<li><a href=\"index.php?act=sections\">Sections</a></li>\n";
		$nav .="<li><a href=\"index.php?act=promos&amp;sector=1\">Promotions</a></li>\n";
		$nav .="<li><a href=\"index.php?act=pos\">Add POS Item</a></li>\n";
		$nav .="<li><a href=\"index.php?act=users\">Users</a></li>\n";
		$nav .="</ul>\n";
		
		return $nav;
	
	}
	
	function displayPromosNav($page = "index.php?act=promos&amp;"){
        require_once("core/sector.class.php");
		$SECTOR = new Sector();
		$array = $SECTOR->getSectors();
		$html = "\n<div id=\"navcontainer\">\n";
		$html .= "<ul class=\"ui-tabs-nav\">\n";
		
		
		foreach ($array as $value) {
			$html .= "<li ".$this->selectActiveNavLI($value['id'])."><a href=\"".$page."sector=".$value['id']."\" ".$this->selectActiveNavA($value['id'])." ><span>".$value['nameOfSector']."</span></a></li>\n";
			
				}
		$html .= "</ul>\n";
		$html .= "</div>\n";
		echo ($html);
        }
        
        function selectActiveNavA($id){
        	if ($id == $_GET['sector']){
				return "class=\"ui-tabs-selected\"";
			}
        }
        function selectActiveNavLI($id){
        	if ($id == $_GET['sector']){
				return "class=\"ui-tabs-selected\"";
			}
        }
	
	
	function buildNavigation(){
	require_once(SITEBASE."/system/core/sector.class.php");
		require_once(SITEBASE."/system/core/category.class.php");
		require_once(SITEBASE."/system/db/dbPromo.class.php");
		$db = new DbPromo();
     	$CATEGORY = new Category();
     	
     	$sql = "SELECT `id` FROM `sectors` WHERE `nameOfSector` = '".$_SESSION['lob']."'";
     	$result = $db->selectQuery($sql);
     	$sectorId = mysql_fetch_array($result);
     	$sector = $sectorId['id'];
		//$categories = $CATEGORY->getCategories($sector);
		
		$sql = "SELECT * FROM `sector_group` WHERE `sector` = '".$sectorId['id']."'";
		$result = $db->selectQuery($sql);
		
		$html = "<div id=\"smoothmenu1\" class=\"ddsmoothmenu\">\n";
		
		if (mysql_num_rows($result) > 0){ // we have the sector in a group
		
		$sectorInfo = mysql_fetch_array($result);
		$groupId = $sectorInfo['sector'];
		$userId = $_SESSION['username'];
		$sql = "SELECT * FROM `users_group` WHERE `groupId` = '".$groupId."' AND `userId` = '".$userId."';";
		$groupresult = $db->selectQuery($sql);
		$groupUserInfo = mysql_fetch_array($groupresult);
		
		if(mysql_num_rows($groupresult) > 0){
			$html .= $this->getNavLevelOne($sector, $CATEGORY, $db);
		}
			
			// id 	groupId 	userId
			
			
			
			
		}else{
			$html .= $this->getNavLevelOne($sector, $CATEGORY, $db);
		}
     	
     	
		//$html .= $this->getNavLevelOne($sector, $CATEGORY, $db);
	//	$html .= "<br style=\"clear: left\" />";
		
		
		
		
		
		$html .= "<br style=\"clear: left\" />";
		$html .= "</div>\n";
		return $html;
	}
	
	function getNavLevelOne($sector, $CATEGORY, $db){
	
		$categories = $CATEGORY->getCategories($sector);
		
		$html .= "<ul>\n";
		foreach($categories as $key => $val){
			
			$html .= "<li><a href=\"#\">".$val['nameOfCategory']."</a>\n";
			$html .= $this->getNavLevelSub($db, $val['id'], $sector);
			$html .= "</li>\n";
		}
		
		$html .= "</ul><!--END OF UL-->\n";
		
		return $html;
	}
	
	
function getNavLevelSub($db, $id, $sector){
	
	$result = $db->selectPromotions($sector,$id);
	
	if (mysql_num_rows($result) > 0 ) {
	
	$catsArray = $this->getSubcategoryArray($db);
	
	while( $row = mysql_fetch_array($result) ){
	$subCategory = $db->getSubCategoryTitle($row['subcategory']);
		if ($subCategory != ""){
			$catsArray[$subCategory][] = $row;
		}else{
			$catsArray[] = $row;
		}
	}
	
	$html .= $this->buildNav($catsArray, $id);
	
	
	}else{
	$html .= "";
	}
	
	return $html;
	}
	
function buildNav($catsArray,$id){
	
		$html .= "<ul class=\"START OF SECONDARY NAV\">\n";
			foreach ($catsArray as $key => $val){
			if ( is_string($key) ){
					if (!empty($val) ){
					$html .= "<li><a href=\"#\">".$key."</a>\n";
						
						$html .= "<ul>\n";
						$html .= $this->subnav($val, $id);
						$html .= "</ul>\n";
						
					$html .= "</li>\n";
						}
				}else{
					$html .= "<li>";

					$html .= "<a href=\"selectimage.php?sec=ps&amp;category=".$id."&amp;promotion=".$val['id']."\">".$val['nameOfPromotion']."</a>\n";
					$html .= "</li>";
				}
	
			}
			$html .= "</ul><!--END OF SECONDARY NAV-->\n";
	//	print_r($html);
		return $html;
	
	}
	
function subnav($catsArray, $id){
		foreach ($catsArray as $key => $val){
				$html .= "<li>\n";
				$html .= "<a href=\"selectimage.php?sec=ps&category=".$id."&amp;promotion=".$val['id']."\">".$val['nameOfPromotion']."</a>\n";
				$html .= "</li>\n";
				}
			return $html;
	}
	
function getSubcategoryArray($db){
		$sql = "SELECT `title` FROM `subcategories`";
     	$catsArray = array();
     	
     	$catsResult = $db->selectQuery($sql);
     	while ($cats = mysql_fetch_array($catsResult)){
     		$catsArray[$cats['title']] = array();
     	}
     	
     	
     	return $catsArray;
     	}
     	
     	
}

?>